package com.lanou.sinasports.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.lanou.sinasports.R;
import com.lanou.sinasports.activity.NbaDecActivity;
import com.lanou.sinasports.beans.Nba;
import com.lanou.sinasports.utils.BitmapCache;
import com.lanou.sinasports.utils.DoubleCache;

import java.util.List;

/**
 * Created by dllo on 16/1/24.
 */
public class NbaAdapter extends BaseAdapter {

    private Context context;
    private List<Nba> data;

    private static final int typeCount = 2;
    private static final int typeOne = 0;
    private static final int typeTwo = 1;
    private int currentType;

    private RequestQueue queue;
    ImageLoader imageLoader;

    public NbaAdapter(Context context, List<Nba> data) {
        this.context = context;
        this.data = data;
        queue = Volley.newRequestQueue(context);
        imageLoader = new ImageLoader(queue, new DoubleCache());
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        currentType = getItemViewType(position);
        if (currentType == typeOne) {
            NbaOneViewHolder oneViewHolder;
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.item_nba_one, parent, false);
                oneViewHolder = new NbaOneViewHolder(convertView);
                convertView.setTag(oneViewHolder);
            } else {
                oneViewHolder = (NbaOneViewHolder) convertView.getTag();
            }

            oneViewHolder.tvStitleOne.setText(data.get(position).getStitle());
            oneViewHolder.tvSummaryOne.setText(data.get(position).getSummary());
            oneViewHolder.tvCountOne.setText(data.get(position).getCount());

            oneViewHolder.imageViewOne.setDefaultImageResId(R.mipmap.ic_main_tab_match_p);
            oneViewHolder.imageViewOne.setErrorImageResId(R.mipmap.ic_main_tab_news_p);
            oneViewHolder.imageViewOne.setImageUrl(data.get(position).getImg(), imageLoader);

        } else if (currentType == typeTwo) {
            NbaTwoViewHolder twoViewHolder;
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.item_nba_two, parent, false);
                twoViewHolder = new NbaTwoViewHolder(convertView);
                convertView.setTag(twoViewHolder);
            } else {
                twoViewHolder = (NbaTwoViewHolder) convertView.getTag();
            }

            twoViewHolder.tvStitleTwo.setText(data.get(position).getStitle());
            twoViewHolder.tvSummaryTwo.setText(data.get(position).getSummary());
            twoViewHolder.tvCountTwo.setText(data.get(position).getPlay_times());

            twoViewHolder.imageViewTwo.setDefaultImageResId(R.mipmap.ic_main_tab_match_p);
            twoViewHolder.imageViewTwo.setErrorImageResId(R.mipmap.ic_main_tab_team_p);
            twoViewHolder.imageViewTwo.setImageUrl(data.get(position).getImg(), imageLoader);

            twoViewHolder.linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Intent intent = new Intent(context, NbaDecActivity.class);
                    intent.putExtra("playlink", data.get(position).getPlaylink());
                    intent.putExtra("vid", data.get(position).getVid());

                    context.startActivity(intent);
                }
            });

        }
        return convertView;
    }


    @Override
    public int getViewTypeCount() {
        return typeCount;
    }

    @Override
    public int getItemViewType(int position) {

        if (data.get(position).getCategoryid().equals("1")) {
            return typeOne;
        } else {
            return typeTwo;
        }

    }

    class NbaOneViewHolder {
        NetworkImageView imageViewOne;
        TextView tvStitleOne;
        TextView tvSummaryOne;
        TextView tvCountOne;

        public NbaOneViewHolder(View view) {

            imageViewOne = (NetworkImageView) view.findViewById(R.id.iv_nba_one_pic);
            tvStitleOne = (TextView) view.findViewById(R.id.tv_nba_one_stitle);
            tvSummaryOne = (TextView) view.findViewById(R.id.tv_nba_one_summary);
            tvCountOne = (TextView) view.findViewById(R.id.tv_nba_one_count);
        }
    }

    class NbaTwoViewHolder {
        LinearLayout linearLayout;
        NetworkImageView imageViewTwo;
        TextView tvTime;
        TextView tvStitleTwo;
        TextView tvSummaryTwo;
        TextView tvCountTwo;

        public NbaTwoViewHolder(View view) {

            imageViewTwo = (NetworkImageView) view.findViewById(R.id.iv_nba_two_pic);
            tvTime = (TextView) view.findViewById(R.id.iv_nba_two_time);
            tvStitleTwo = (TextView) view.findViewById(R.id.tv_nba_two_stitle);
            tvSummaryTwo = (TextView) view.findViewById(R.id.tv_nba_two_summary);
            tvCountTwo = (TextView) view.findViewById(R.id.tv_nba_two_count);
            linearLayout = (LinearLayout) view.findViewById(R.id.ll_nba_two);
        }
    }
}
